<?php

namespace app\api\model;

use think\Model;

class ImFriendGroup extends Model
{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * Notes:添加好友分组
     * User: tangfen
     * Date: 2023/12/23
     * Time: 14:33
     * @param $params
     */
    public function addFriendGroup($params)
    {
        $result = $this->where(['uid' => $params['uid'], 'friend_group_name' => $params['friend_group_name']])->value('id');
        if (empty($result)) {
            //不存在好友分组添加记录
            $this->data($params);
            $result = $this->save();
            if ($result) {
                return $this->id;
            } else {
                return false;
            }
        } else {
            $this->where(['uid' => $params['uid'], 'friend_group_name' => $params['friend_group_name']])->update(['status' => 1, 'update_time' => time()]);//存在好友分组是更新状态
            return $result;
        }
    }

    /**
     * Notes:好友分组列表
     * User: tangfen
     * Date: 2023/12/23
     * Time: 15:49
     */
    public function groupFriendList($params)
    {
        $where = [
            'uid' => $params['uid'],
            'status' => 1
        ];

        $list = $this->where($where)
            ->field('*')
            ->order('id desc')
            ->paginate($params['limit'], false, ['page' => $params['page']]);
        return $list;

    }


    /**
     * Notes:删除好友分组
     * User: tangfen
     * Date: 2023/12/21
     * Time: 20:39
     * @param $params
     * @return bool
     */
    public function delGroupFriend($id)
    {
        $result = $this->where(['id' => $id])->update(['status' => 2]);// 1正常, 2解散
        if ($result !== false) {
            return true;
        } else {
            return false;
        }
    }

}